package com.wushi.lenovo.asia5b.app_ui.other.dialog.adapter;

import android.content.Context;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.wushi.lenovo.asia5b.R;
import com.wushi.lenovo.asia5b.app_model.credit.BaseHisModel;
import com.wushi.lenovo.asia5b.app_utils.WFileUtil;

import java.io.File;
import java.util.List;

import static timber.log.Timber.d;

/**
 * Created by zhangyuncai on 2019/11/4.
 */
public class DownPdfAdapter extends BaseQuickAdapter<BaseHisModel, BaseViewHolder> {

    public DownPdfAdapter(@Nullable List<BaseHisModel> data) {
        super(R.layout.item_downpdf_adapter, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, BaseHisModel item) {
        helper.setText(R.id.tv_downpdf_time, item.getCreated_at());
        helper.setText(R.id.tv_downpdf_no, item.getReference_number());
        TextView tv_downloadtrans_view = helper.getView(R.id.tv_downpdf_view);
        //pdf下载地址是否存在
        String filePath = fileExists(mContext,item.getReference_number());
        tv_downloadtrans_view.setVisibility(!TextUtils.isEmpty(filePath) ? View.VISIBLE : View.GONE);
        tv_downloadtrans_view.setTag(filePath);
        helper.addOnClickListener(R.id.iv_downpdf_downicon);
        helper.addOnClickListener(R.id.tv_downpdf_view);
    }

    //这里不需要权限,文件是否存在,先判断pdf文件,再判断img文件
    private String fileExists(Context context,String reference_number) {
        String pdfPath = WFileUtil.createPdfPath(context, reference_number);
        File file = new File(pdfPath);
        d("--->>>>>>>>---pdfPath是否存在:" + file.exists());
        if (file.exists()) {
            return pdfPath;
        } else {
            String imgPath = WFileUtil.createImagePath(context, reference_number);
            file = new File(imgPath);
            if (file.exists()) {
                return imgPath;
            }
        }
        return null;
    }
}
